package dao.impl;

import dao.BookDao;
import entity.Book;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.qf.utils.DruidUtil;

import java.sql.SQLException;
import java.util.List;

/*
杨威
2021/9/7
21:26
*/
public class BookDaoImpl implements BookDao {
    QueryRunner queryRunner=new QueryRunner();
    @Override
    public int insert(Book book) {
        try {
            String sql="insert into book(btitle,bauthor,bpublicdate,bpublisher,bisbn,bprice) VALUES (?,?,?,?,?,?) ";
            Object[] params={book.getbTitle(),book.getbAuthor(),book.getbPublicDate(),book.getbPublisher(),book.getbIsbn(),book.getbPrice()};
            int update = queryRunner.update(DruidUtil.getConnection(), sql, params);
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"新增书籍数量出现异常");
        }
    }

    @Override
    public int delete(int i) {
        try {
            String sql="delete from book where bid=?";
            return queryRunner.update(DruidUtil.getConnection(),sql,i);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"删除书籍根据ID出现异常");
        }

    }

    @Override
    public int update(Book book) {
        try {
            String sql="update book set btitle=? where bid=?";
            int update = queryRunner.update(DruidUtil.getConnection(), sql, book.getbTitle(), book.getbId());
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"修改书籍出现异常");
        }
    }

    @Override
    public Book selectById(int i) {
        try {
            String sql="select * from book where bid=?";
            Book query = queryRunner.query(DruidUtil.getConnection(), sql, new BeanHandler<>(Book.class), i);
            return query;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"查询单个书籍根据ID出现异常");
        }
    }


    @Override
    public List<Book> selectAll() {
        try {
            String sql="select * from book";
            List<Book> query = queryRunner.query(DruidUtil.getConnection(), sql, new BeanListHandler<>(Book.class));
            return query;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"查询所有书籍出现异常");
        }
    }

    @Override
    public long selectCount() {
        try {
            String sql="select count(*) from book";
            return queryRunner.query(DruidUtil.getConnection(),sql,new ScalarHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"查询书籍数量出现异常");
        }
    }

    @Override
    public List<Book> selectAllOrderByDate() {
        try {
            String sql="select * from book order by bPublicDate";
            List<Book> query = queryRunner.query(DruidUtil.getConnection(), sql, new BeanListHandler<>(Book.class));
            return query;
        } catch (SQLException e) {
           throw new RuntimeException(e.getMessage()+"查询所有书籍根据日期排序出现异常！");
        }
    }
}
